package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.clients.CityClient;
import cn.kgc.springcloud.clients.ClazzClient;
import cn.kgc.springcloud.clients.GroupClient;
import cn.kgc.springcloud.clients.TagClient;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.form.StudentForm;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.utils.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mengshun
 * @description 针对表【t_student】的数据库操作Service实现
 * @createDate 2025-01-17 09:31:49
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private CityClient cityClient;
    @Resource
    private ClazzClient clazzClient;
    @Resource
    private GroupClient groupClient;
    @Resource
    private TagClient tagClient;
    @Resource
    private StudentTagMapper studentTagMapper;

    @Override
    public List<StudentVO> studentList() {

        List<Student> students = baseMapper.selectList(null);
        // 遍历学生列表   student  -> studentVO
        List<StudentVO> studentVOS = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // 发送http请求   城市服务
            Integer cityId = studentVO.getCityid();
            City city = cityClient.getCity(cityId);
            studentVO.setCity(city);

            // 发送http请求   班级服务
            Integer clazzId = student.getClazzid();
            Clazz clazz = clazzClient.getClazzById(clazzId);
            studentVO.setClazz(clazz);

            // 发送http请求   小组服务
            Integer groupId = student.getGroupid();
            Group group = groupClient.getGroup(groupId);
            studentVO.setGroup(group);

            // 发送http请求   标签服务
            Integer sid = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,sid);

            List<Integer> tIds = studentTagMapper.selectList(lambda).stream()
                    .map(studentTag -> studentTag.getTid())
                    .collect(Collectors.toList());

            List<Tag> tags = tIds.stream().map(tid -> {
                Tag tag = tagClient.getTagById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return studentVOS;
    }


    @Transactional
    @Override
    public StudentVO addStudent(StudentForm studentForm) {

        // 获取学生表中的数据
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);

        // 处理学生 年龄 星座 属相 相关数据   birth
        Date bir = student.getBir();
        String attr = DateUtil.getYear(bir);
        String starts = DateUtil.getConstellation(bir);
        Integer age = DateUtil.getAge(bir);

        student.setStarts(starts);
        student.setAge(age);
        student.setAttr(attr);
        baseMapper.insert(student);

        // 学生和标签关联表数据   sid
        Integer sid = student.getId();
        List<Integer> tagIds = studentForm.getTagIds();

        tagIds.forEach(tid->{
            StudentTag studentTag = new StudentTag();
            studentTag.setSid(sid);
            studentTag.setTid(tid);
            studentTagMapper.insert(studentTag);
        });


        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student,studentVO);

        // 1.城市信息
        Integer cityId = student.getCityid();
        City city = cityClient.getCity(cityId);
        studentVO.setCity(city);

        // 2. 小组信息
        Integer groupId = student.getGroupid();
        Group group = groupClient.getGroup(groupId);
        studentVO.setGroup(group);

        // 3. 班级信息
        Integer clazzId = student.getClazzid();
        Clazz clazz = clazzClient.getClazzById(clazzId);
        studentVO.setClazz(clazz);

        // 4.标签信息
        List<Tag> tags = tagIds.stream().map(tid -> {
            Tag tag = tagClient.getTagById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);

        return studentVO;
    }
}




